<?php

declare(strict_types=1);
/**
 * This file is part of Hyperf.
 *
 * @link     https://www.hyperf.io
 * @document https://hyperf.wiki
 * @contact  group@hyperf.io
 * @license  https://github.com/hyperf/hyperf/blob/master/LICENSE
 */
namespace App\Controller;

use App\Model\Order;
use App\Model\User;
use Hyperf\DbConnection\Db;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\RequestMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Contract\RequestInterface;


/**
 * @Controller()
 */
class OrdersController extends AbstractController
{

    /**
     * @return array
     * @RequestMapping(path="getList", methods="get,post")
     *
     */
    public function getList(): array
    {
        $users = User::query()->where('id',1)->get()->toArray();

        return $users;
    }

    /**
     * @return array
     * @RequestMapping(path="getOne", methods="get,post")
     *
     */
    public function getOne(): array
    {
        $users = Order::query()->where('id',1)->get()->toArray();

        return $users;
    }

    /**
     * @return array
     * @RequestMapping(path="save", methods="get,post")
     *
     */
    public function save(RequestInterface $request): array
    {
       $username = $request->query('username');
       $email = $request->query('email');
       $res = Db::table('users')->insert([
           ['email' => $email, 'username' => $username]
       ]);
        return [
            'res' => $res,
            'message' => "success",
        ];
    }

    /**
     * @return array
     * @RequestMapping(path="del", methods="get,post")
     *
     */
    public function del(RequestInterface $request): array
    {
        $id = (int)$request->query("id");
        $res = Db::table('users')->where('id', '=', $id)->delete();
        return [
            'res' => $res,
            'message' => "success",
        ];
    }

}
